﻿using System;
using System.Collections.Generic;
using ProjScan.Tools;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Submodules.Effects_Play_Check
{
	// Token: 0x02000194 RID: 404
	internal class ParticleSystemDrawer : IDisposable
	{
		// Token: 0x06000DC8 RID: 3528 RVA: 0x0003DDF4 File Offset: 0x0003BFF4
		public ParticleSystemDrawer(GameObject selectedGameObject)
		{
			this.modelGameObject = selectedGameObject;
			this.Initialize();
		}

		// Token: 0x06000DC9 RID: 3529 RVA: 0x00006F8F File Offset: 0x0000518F
		public void Dispose()
		{
			this.modelGameObject = null;
			EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.UpdateFunction));
		}

		// Token: 0x06000DCA RID: 3530 RVA: 0x0003DE44 File Offset: 0x0003C044
		public void Initialize()
		{
			this.updateCounter = 2;
			this.boundsAssigned = false;
			this.particleSystems = this.modelGameObject.GetComponentsInChildren<ParticleSystem>();
			this.currentBounds = default(Bounds);
			this.currentBounds.center = this.modelGameObject.transform.position;
			bool flag = this.particleSystems.Length != 0;
			if (flag)
			{
				this.currentBounds.center = this.particleSystems[0].GetComponent<Renderer>().bounds.center;
			}
			this.hasBounds = true;
			EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(this.UpdateFunction));
			ApiCompatibilityUtils.Instance.RegisterEditorExitPlayMode(new Action(this.ResetEditorUpdate));
		}

		// Token: 0x06000DCB RID: 3531 RVA: 0x00006FB9 File Offset: 0x000051B9
		private void ResetEditorUpdate()
		{
			EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.UpdateFunction));
		}

		// Token: 0x06000DCC RID: 3532 RVA: 0x0003DF10 File Offset: 0x0003C110
		public void UpdateFunction()
		{
			bool flag = this.modelGameObject == null;
			if (flag)
			{
				EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.UpdateFunction));
			}
			else
			{
				this.UpdateBounds();
				this.UpdateVertices();
				this.UpdateGizmoLines();
			}
		}

		// Token: 0x06000DCD RID: 3533 RVA: 0x0003DF6C File Offset: 0x0003C16C
		private void UpdateBounds()
		{
			int num = this.updateCounter;
			this.updateCounter = num - 1;
			bool flag = num == 0 && !this.boundsAssigned;
			if (flag)
			{
				Vector3 vector = Vector3.zero;
				foreach (ParticleSystem particleSystem in this.particleSystems)
				{
					try
					{
						this.helperBounds = particleSystem.GetComponent<Renderer>().bounds;
						vector += this.helperBounds.center;
					}
					catch (Exception ex)
					{
						Debug.LogError(ex);
					}
				}
				this.currentBounds.center = vector / (float)this.particleSystems.Length;
				this.boundsAssigned = true;
			}
			bool flag2 = this.boundsAssigned;
			if (flag2)
			{
				ParticleSystem[] array2 = this.particleSystems;
				int j = 0;
				while (j < array2.Length)
				{
					ParticleSystem particleSystem2 = array2[j];
					try
					{
						this.helperBounds = particleSystem2.GetComponent<Renderer>().bounds;
					}
					catch (Exception ex2)
					{
						Debug.LogError(ex2);
						goto IL_102;
					}
					goto IL_EF;
					IL_102:
					j++;
					continue;
					IL_EF:
					this.currentBounds.Encapsulate(this.helperBounds);
					goto IL_102;
				}
			}
		}

		// Token: 0x06000DCE RID: 3534 RVA: 0x0003E0A8 File Offset: 0x0003C2A8
		private void UpdateVertices()
		{
			this.topFrontRight = Vector3.Scale(this.currentBounds.extents, new Vector3(1f, 1f, 1f));
			this.topFrontLeft = Vector3.Scale(this.currentBounds.extents, new Vector3(-1f, 1f, 1f));
			this.topBackLeft = Vector3.Scale(this.currentBounds.extents, new Vector3(-1f, 1f, -1f));
			this.topBackRight = Vector3.Scale(this.currentBounds.extents, new Vector3(1f, 1f, -1f));
			this.bottomFrontRight = Vector3.Scale(this.currentBounds.extents, new Vector3(1f, -1f, 1f));
			this.bottomFrontLeft = Vector3.Scale(this.currentBounds.extents, new Vector3(-1f, -1f, 1f));
			this.bottomBackLeft = Vector3.Scale(this.currentBounds.extents, new Vector3(-1f, -1f, -1f));
			this.bottomBackRight = Vector3.Scale(this.currentBounds.extents, new Vector3(1f, -1f, -1f));
			this.boundingVertices = new Vector3[]
			{
				this.topFrontRight,
				this.topFrontLeft,
				this.topBackLeft,
				this.topBackRight,
				this.bottomFrontRight,
				this.bottomFrontLeft,
				this.bottomBackLeft,
				this.bottomBackRight
			};
		}

		// Token: 0x06000DCF RID: 3535 RVA: 0x0003E27C File Offset: 0x0003C47C
		private void UpdateGizmoLines()
		{
			Vector3 center = this.currentBounds.center;
			List<Vector3[]> list = new List<Vector3[]>();
			for (int i = 0; i < 4; i++)
			{
				Vector3[] item = new Vector3[]
				{
					this.boundingVertices[2 * i] + center,
					this.boundingVertices[2 * i + 1] + center
				};
				list.Add(item);
				item = new Vector3[]
				{
					this.boundingVertices[i] + center,
					this.boundingVertices[i + 4] + center
				};
				list.Add(item);
				item = new Vector3[]
				{
					this.boundingVertices[2 * i] + center,
					this.boundingVertices[2 * i + 3 - 4 * (i % 2)] + center
				};
				list.Add(item);
			}
			this.gizmoLines = new Vector3[list.Count, 2];
			for (int j = 0; j < list.Count; j++)
			{
				this.gizmoLines[j, 0] = list[j][0];
				this.gizmoLines[j, 1] = list[j][1];
			}
		}

		// Token: 0x06000DD0 RID: 3536 RVA: 0x0003E3F4 File Offset: 0x0003C5F4
		private void DrawGizmos()
		{
			bool flag = this.hasBounds;
			if (flag)
			{
				Gizmos.color = this.gizmosColor;
				for (int i = 0; i < this.gizmoLines.GetLength(0); i++)
				{
					Gizmos.DrawLine(this.gizmoLines[i, 0], this.gizmoLines[i, 1]);
				}
			}
		}

		// Token: 0x17000346 RID: 838
		// (get) Token: 0x06000DD1 RID: 3537 RVA: 0x0003E458 File Offset: 0x0003C658
		public Bounds BoundingBox
		{
			get
			{
				return this.currentBounds;
			}
		}

		// Token: 0x04000888 RID: 2184
		public GameObject modelGameObject;

		// Token: 0x04000889 RID: 2185
		public bool isDisposed = false;

		// Token: 0x0400088A RID: 2186
		private bool hasBounds = false;

		// Token: 0x0400088B RID: 2187
		public Color gizmosColor = new Color(0f, 1f, 0.4f, 0.74f);

		// Token: 0x0400088C RID: 2188
		public Bounds currentBounds;

		// Token: 0x0400088D RID: 2189
		[HideInInspector]
		private Bounds helperBounds;

		// Token: 0x0400088E RID: 2190
		private Vector3[] boundingVertices;

		// Token: 0x0400088F RID: 2191
		private Vector3[,] gizmoLines;

		// Token: 0x04000890 RID: 2192
		private Vector3 topFrontRight;

		// Token: 0x04000891 RID: 2193
		private Vector3 topFrontLeft;

		// Token: 0x04000892 RID: 2194
		private Vector3 topBackLeft;

		// Token: 0x04000893 RID: 2195
		private Vector3 topBackRight;

		// Token: 0x04000894 RID: 2196
		private Vector3 bottomFrontRight;

		// Token: 0x04000895 RID: 2197
		private Vector3 bottomFrontLeft;

		// Token: 0x04000896 RID: 2198
		private Vector3 bottomBackLeft;

		// Token: 0x04000897 RID: 2199
		private Vector3 bottomBackRight;

		// Token: 0x04000898 RID: 2200
		private Vector3 tempVertex;

		// Token: 0x04000899 RID: 2201
		private ParticleSystem[] particleSystems;

		// Token: 0x0400089A RID: 2202
		public bool boundsAssigned;

		// Token: 0x0400089B RID: 2203
		private int updateCounter;
	}
}
